using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class MP_UI : MonoBehaviour
{
    public PlayerStat playerStat;
    private Slider slider;
    private TextMeshProUGUI text;

    void Start()
    {
        slider = GetComponent<Slider>();
        text = GetComponentInChildren<TextMeshProUGUI>();
        if (playerStat != null) playerStat.onChangeMP += UpdateUI;
        UpdateUI();
    }

    void Update()
    {
        if (playerStat != null && playerStat.magicIsRecovery)
            UpdateUI();
    }

    private void UpdateUI()
    {
        if (playerStat == null) return;

        int maxValue = playerStat.GetMaxMP();
        float value = playerStat.GetMP();
        slider.maxValue = maxValue;
        slider.value = value;
        text.text = (value.ToString("0") + "/" + maxValue.ToString());
    }

    private void OnDisable()
    {
        playerStat.onChangeMP -= UpdateUI;
    }
}
